package com.heima.wemedia;

import com.heima.file.service.FileStorageService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

@SpringBootTest
public class OssDemo {
    @Autowired
    private FileStorageService fileStorageService;
    @Value("${file.oss.prefix}")
    String prefix;
    @Value("${file.oss.web-site}")
    String webSite;

    /**
     * 上传图片
     * @throws FileNotFoundException
     */
    @Test
    public void uploadPic() throws FileNotFoundException {
        //上传文件流
        FileInputStream fileInputStream = new FileInputStream("D:\\bizhi\\dfdaa8ba84a97e58891abeab1e9e6351730a44a9.jpg@942w_576h_progressive.webp.jpg");
        //阿里云oss文件目录、文件名称、上传文件地址
        //返回值：oss上文件的完整目录
        String store = fileStorageService.store(prefix, "mv001.jpg", fileInputStream);
        System.out.println(webSite+store);
    }

    /**
     * 删除文件
     * @throws FileNotFoundException
     */
    @Test
    public void deletePic() throws FileNotFoundException {
       fileStorageService.delete("material/2022/7/20220704/mv001.jpg");
    }
}
